package com.xcyy.clbzbh.service.imp;

import com.xcyy.clbzbh.VO.QueryParam;
import com.xcyy.clbzbh.entity.Benchmark;
import com.xcyy.clbzbh.repository.BenchmarkRepository;
import com.xcyy.clbzbh.service.BenchmarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class BenchmarkServiceImp implements BenchmarkService {
    @Autowired
    private BenchmarkRepository benchmarkRepository;

    @Override
    public Page<Benchmark> findAll(QueryParam queryParam,int pageNum, int pageSize) {
        Pageable pageable=PageRequest.of(pageNum,pageSize,Sort.by(Sort.Direction.ASC,"pointID"));
        Page<Benchmark> benchmarkPage= benchmarkRepository.findAll(new Specification<Benchmark>() {

            @Override
            public Predicate toPredicate(Root<Benchmark> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

                List<Predicate> predicates = new ArrayList<Predicate>();
                if (queryParam.getName()!=null&&!"".equals(queryParam.getName())){
                    predicates.add(cb.like(root.get("name"),"%"+queryParam.getName()+"%"));
                }
                if (queryParam.getDh()!=null&&!"".equals(queryParam.getDh())){
                    predicates.add(cb.equal(root.get("pointID"),queryParam.getDh()));
                }
                if (queryParam.getGrade()!=null&&!"".equals(queryParam.getGrade())){
                    predicates.add(cb.equal(root.get("grade"),queryParam.getGrade()));
                }
                if (queryParam.getCity()!=null&&!"".equals(queryParam.getCity())){
                    predicates.add(cb.equal(root.get("city"),queryParam.getCity()));
                }
                if (queryParam.getCountry()!=null&&!"".equals(queryParam.getCountry())){
                    predicates.add(cb.equal(root.get("country"),queryParam.getCountry()));
                }
                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        },pageable);
        return benchmarkPage;
    }

    @Override
    public Benchmark save(Benchmark benchmark) {
        return benchmarkRepository.save(benchmark);
    }

    @Override
    public Benchmark findById(String id) {
        return benchmarkRepository.findById(id).orElse(null);
    }

    @Override
    public Object totalbenchmark() {
        return benchmarkRepository.totalbenchmark();
    }

    @Override
    public List<Object> countCity() {
        return benchmarkRepository.countCity();
    }

    @Override
    public List<Object> countCountry(String city) {
        return benchmarkRepository.countCountry(city);
    }

    @Override
    public List<Benchmark> findByCountry(String country) {
        return benchmarkRepository.findByCountry(country);
    }

    @Override
    public Page<Object> countAllCountry(Pageable pageable) {
        return benchmarkRepository.countAllCountry(pageable);
    }

    @Override
    public List<Object> countByName(String country) {
        return benchmarkRepository.countByName(country);
    }
}
